package cn.jyc.ssm03.ctrl;

import cn.jyc.ssm03.pojo.Role;
import cn.jyc.ssm03.pojo.User;
import cn.jyc.ssm03.service.CustService;
import cn.jyc.ssm03.service.MenuListService;
import cn.jyc.ssm03.service.RolesService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Controller
public class RolesCtrl {
    @Autowired
    private RolesService rs;
    @Autowired
    private MenuListService mls;
    @Autowired
    private CustService cs;

    @ResponseBody
    @RequestMapping("/rolelist")
    public List<Role> rolelist() {
        return rs.qryAll();
    }

    @ResponseBody
    @RequestMapping("/menuList")
    public String menuList() {
        return mls.getMenu();
    }

    @ResponseBody
    @RequestMapping("/saveRoleMenu")
    public Map<String, Object> saveMenuList(Integer rid, Integer[] mids) {
        return rs.saveRoleMenu(rid, mids);
    }

    @ResponseBody
    @RequestMapping("/roleMenuId")
    public List<Integer> roleMenuId(Integer rid) {
        return rs.getRoleMenuId(rid);
    }

    @ResponseBody
    @RequestMapping("/getUsers")
    public Map<String, Object> getUsers(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        return rs.getUsers(page, rows);
    }

    @ResponseBody
    @RequestMapping("/addUser")
    public Map<String, Object> addUser(User u, Integer[] rids) {
        return rs.addUser(u, rids);
    }

    @ResponseBody
    @RequestMapping("/qrtURoles")
    public List<Integer> qrtURoles(Integer uid) {
        return rs.qryURoles(uid);
    }

    @ResponseBody
    @RequestMapping("/updUser")
    public Map<String, Object> updUser(User u, Integer[] rids) {
        return rs.updUser(u, rids);
    }

    @ResponseBody
    @RequestMapping("/delUser")
    public Map<String, Object> delUser(Integer uid) {
        return rs.delUser(uid);
    }

    @RequestMapping("/loginq")
    public String loginq(String ln, String pwd, HttpSession session) {
//        System.out.println("登录的用户名密码分别为：" + ln + "、" + pwd);
        User user = rs.qryUserByName(ln, pwd);
//        System.out.println("查询到的用户为：" + user);
        if (user == null) {
            return "redirect:index.jsp";
        }
        session.setAttribute("unm", user.getLoginname());
        session.setAttribute("uid", user.getUserid());
        return "main";
    }

    @RequestMapping("getCustExcel")
    public ResponseEntity<byte[]> getCustExcel() throws IOException {
        XSSFWorkbook sheets = cs.exportCusExcel();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        sheets.write(baos);
        byte[] bytes = baos.toByteArray();
        String filename="客户信息"+ LocalDate.now()+".xlsx";
        filename= URLEncoder.encode(filename,"UTF-8");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("ContentType","application/ms-excel;charset=UTF-8");
        httpHeaders.add("Content-Disposition","attachment;filename="+filename);
        ResponseEntity<byte[]> res = new ResponseEntity<>(bytes, httpHeaders, HttpStatus.CREATED);
        return res;
    }

    @RequestMapping("importCust")
    @ResponseBody
    public Map<String,Object> importCust(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        return cs.importCusExcel(workbook);
    }
}
